package hu.cskassai.sad.gui.panels;

import hu.cskassai.sad.business.GraphManager;
import hu.cskassai.sad.business.requests.ChangeAlgorithmRequest;
import hu.cskassai.sad.business.requests.ChangeGraphRequest;
import hu.cskassai.sad.business.requests.ChangeHeuristicRequest;
import hu.cskassai.sad.gui.frames.MainFrame;
import hu.cskassai.sad.model.graph.Edge;
import hu.cskassai.sad.model.graph.Vertex2D;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class AlgorithmChooserPanel extends JPanel {
	
	protected static final long PERIOD = 100;
	
	private final MainFrame mainFrame;
	
	private final GraphManager<Vertex2D, Edge> graphManager;
	
	private final JComboBox heuristicChooser;
	
	private final JComboBox algorithmChooser;
	
	private final JComboBox graphChooser;
	
	private Timer timer;
	
	/**
	 * Create the panel.
	 */
	public AlgorithmChooserPanel(final MainFrame mainFrame, final GraphManager<Vertex2D, Edge> graphManager) {
		super();
		setLayout(null);
		this.mainFrame = mainFrame;
		this.graphManager = graphManager;
		algorithmChooser = new JComboBox();
		
		algorithmChooser.setBounds(50, 80, 200, 25);
		algorithmChooser.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent arg0) {
				final String selectedItem = (String) algorithmChooser.getSelectedItem();
				final ChangeAlgorithmRequest request = new ChangeAlgorithmRequest();
				request.setName(selectedItem);
				graphManager.changeAlgorithm(request);
				mainFrame.changeAlgorithm();
				
			}
		});
		add(algorithmChooser);
		
		heuristicChooser = new JComboBox();
		heuristicChooser.setBounds(350, 80, 200, 25);
		
		heuristicChooser.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent arg0) {
				final String selectedItem = (String) heuristicChooser.getSelectedItem();
				final ChangeHeuristicRequest request = new ChangeHeuristicRequest();
				request.setName(selectedItem);
				graphManager.changeHeuristic(request);
				mainFrame.changeAlgorithm();
				
			}
		});
		add(heuristicChooser);
		
		graphChooser = new JComboBox();
		graphChooser.setBounds(650, 80, 200, 25);
		
		add(graphChooser);
		
		fillItems();
		graphChooser.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent arg0) {
				final String selectedItem = (String) graphChooser.getSelectedItem();
				final ChangeGraphRequest request = new ChangeGraphRequest();
				request.setName(selectedItem);
				graphManager.changeGraph(request);
				mainFrame.changeGraph();
				
			}
		});
		
		final JButton newAlgorithmButton = new JButton();
		newAlgorithmButton.setIcon(new ImageIcon(AlgorithmChooserPanel.class.getResource("/icons/plus.jpg")));
		newAlgorithmButton.setBounds(260, 80, 25, 25);
		newAlgorithmButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent e) {
				final AddAlgorithmDialog addAlgorithmDialog = new AddAlgorithmDialog(mainFrame, algorithmChooser,
					graphManager);
				addAlgorithmDialog.setVisible(true);
				
			}
		});
		add(newAlgorithmButton);
		
		final JButton nweHeuristicButton = new JButton();
		nweHeuristicButton.setIcon(new ImageIcon(AlgorithmChooserPanel.class.getResource("/icons/plus.jpg")));
		nweHeuristicButton.setBounds(560, 80, 25, 25);
		nweHeuristicButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent e) {
				final AddHeuristicDialog addHeuristicDialog = new AddHeuristicDialog(mainFrame, heuristicChooser,
					graphManager);
				addHeuristicDialog.setVisible(true);
				
			}
		});
		add(nweHeuristicButton);
		
		final JButton newGraphButton = new JButton();
		newGraphButton.setIcon(new ImageIcon(AlgorithmChooserPanel.class.getResource("/icons/plus.jpg")));
		newGraphButton.setBounds(860, 80, 25, 25);
		newGraphButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent e) {
				final AddGraphDialog addGraphDialog = new AddGraphDialog(mainFrame, graphChooser, graphManager);
				addGraphDialog.setVisible(true);
				
			}
		});
		add(newGraphButton);
		
		final JButton stepButton = new JButton();
		stepButton.setIcon(new ImageIcon(AlgorithmChooserPanel.class.getResource("/icons/step play.png")));
		stepButton.setBounds(1100, 80, 25, 25);
		stepButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent e) {
				AlgorithmChooserPanel.this.step();
				
			}
		});
		add(stepButton);
		
		final JButton playButton = new JButton();
		playButton.setIcon(new ImageIcon(AlgorithmChooserPanel.class.getResource("/icons/play.png")));
		playButton.setBounds(1150, 80, 25, 25);
		playButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent e) {
				timer = new Timer();
				final TimerTask timerTask = new TimerTask() {
					
					@Override
					public void run() {
						AlgorithmChooserPanel.this.step();
						
					}
				};
				timer.schedule(timerTask, 0, PERIOD);
				
			}
		});
		add(playButton);
		
		final JButton pauseButton = new JButton();
		pauseButton.setIcon(new ImageIcon(AlgorithmChooserPanel.class.getResource("/icons/pause.png")));
		pauseButton.setBounds(1200, 80, 25, 25);
		pauseButton.addActionListener(new ActionListener() {
			
			public void actionPerformed(final ActionEvent e) {
				timer.cancel();
				
			}
		});
		add(stepButton);
		add(pauseButton);
		
		final JButton stopButton = new JButton();
		stopButton.setIcon(new ImageIcon(AlgorithmChooserPanel.class.getResource("/icons/stop.jpg")));
		stopButton.setBounds(1250, 80, 25, 25);
		add(stopButton);
		
		final JLabel lblAlgoritmusok = new JLabel("Algoritmusok:");
		lblAlgoritmusok.setFont(new Font("Dialog", Font.BOLD, 14));
		lblAlgoritmusok.setBounds(30, 50, 120, 15);
		add(lblAlgoritmusok);
		
		final JLabel lblHeurisztikak = new JLabel("Heurisztikák:");
		lblHeurisztikak.setFont(new Font("Dialog", Font.BOLD, 14));
		lblHeurisztikak.setBounds(330, 50, 120, 15);
		add(lblHeurisztikak);
		
		final JLabel lblGrafok = new JLabel("Gráfok:");
		lblGrafok.setFont(new Font("Dialog", Font.BOLD, 14));
		lblGrafok.setBounds(630, 50, 120, 15);
		add(lblGrafok);
		
	}
	
	public void refresh() {
		algorithmChooser.removeAllItems();
		graphChooser.removeAllItems();
		heuristicChooser.removeAllItems();
		fillItems();
		
	}
	
	private void fillItems() {
		final Collection<String> algorithms = graphManager.getAlgorithms();
		for (final String string : algorithms) {
			algorithmChooser.addItem(string);
		}
		final Collection<String> graphs = graphManager.getGraphs();
		for (final String string : graphs) {
			graphChooser.addItem(string);
		}
		final Collection<String> heuristics = graphManager.getHeuristic();
		for (final String string : heuristics) {
			heuristicChooser.addItem(string);
		}
		
	}
	
	private void step() {
		graphManager.step();
		mainFrame.stepRefresh();
		mainFrame.repaint();
	}
}
